package com.imay.live.helper;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.imay.cocos.IMayCocos;
import com.imay.cocos.IMayCocosNetManager;
import com.imay.live.cache.UserInfoCache;
import com.imay.live.model.HouseH5Model;
import com.imay.live.model.UserInfoModel;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.proto.helper.ProtoSocketCallBack;
import com.imay.live.proto.helper.ProtoSocketFactory;
import com.imay.live.utils.ImayLogUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 建楼卡
 * Created by lzx on 2017/07/24 0024.
 */

public class RoomHouseHelper {
    private int rData = -1;
    private int userLv = 1;

    private OnRoomHouseListener onRoomHouseListener;

    public interface OnRoomHouseListener {
        void onShowUI(int rData, int userLv);

        void onClose(int receiveStatus);

        void onOpenH5(String url);
    }

    public void setData(int rData, int userLv) {
        this.rData = rData;
        this.userLv = userLv;
    }

    public int getrData() {
        return rData;
    }

    public int getUserLv() {
        return userLv;
    }

    public void showHouseCard() {
        IMayCocosNetManager.getInstance().addOnCocosRoomHouseListener(new IMayCocosNetManager.OnCocosRoomHouseListener() {

            @Override
            public void onOpenH5(String json) {
                HouseH5Model houseH5Model = JSON.parseObject(json, HouseH5Model.class);
                if (houseH5Model != null && !TextUtils.isEmpty(houseH5Model.getUrl())) {
                    onRoomHouseListener.onOpenH5(houseH5Model.getUrl());
                }
                onClose();
            }

            @Override
            public void onClose() {
                IMayCocosNetManager.getInstance().removeOnCocosRoomHouseListener();
                onRoomHouseListener.onClose(0);
            }
        });
        IMayCocos.getInstance().showHouseCard(rData, userLv);
        PreferenceHelper.getInstance().putLong(PreferenceHelper.ROOMHOUSE_TIME, System.currentTimeMillis());
        rData = -1;
    }

    public void getHouseCard(final OnRoomHouseListener onRoomHouseListener) {
        if (onRoomHouseListener == null) {
            return;
        }
        this.onRoomHouseListener = onRoomHouseListener;
        ProtoRequestHelper.task(ProtoSocketFactory.requestRoomHouse()).setTag(this.hashCode()).requestFullData(new ProtoSocketCallBack() {
            @Override
            public void onRequestSuccess(String command, int msgId, String data) {
                ImayLogUtil.log(data);
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    if (jsonObject.has("ReceiveStatus")) {
                        int receiveStatus = jsonObject.getInt("ReceiveStatus");
                        if (receiveStatus == 0) {
                            UserInfoModel userInfo = UserInfoCache.getUserInfo();
                            if (userInfo != null && userInfo.getUser() != null) {
                                onRoomHouseListener.onShowUI(0, userInfo.getUser().getUserLevel());
                                return;
                            }

                        }
                    }else {
                        ImayLogUtil.log("ReceiveStatus");
                        UserInfoModel userInfo = UserInfoCache.getUserInfo();
                        if (userInfo != null && userInfo.getUser() != null) {
                            ImayLogUtil.log("ReceiveStatus show");
                            onRoomHouseListener.onShowUI(0, userInfo.getUser().getUserLevel());
                            return;
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                onRoomHouseListener.onClose(1);
            }

            @Override
            public void onRequestError(String command, int code, String message) {
                onRoomHouseListener.onClose(1);
            }
        });
    }
}
